/*
    This file is part of SQLiteGTK, written by Aly Hirani

    SQLiteGTK is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    SQLiteGTK is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with SQLiteGTK.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "include.h"
#include "main_window_notebook.h"
#include "main_window_notebook_tables_list.h"
#include "util.h"

/* FIXME: The following code should be moved into the master_page file */
	/*
	event_box = GTK_WIDGET(gtk_builder_get_object(builder, "editor_notebook_page_master_hint_event_box"));
	g_assert(event_box != NULL);
	
	GdkColor event_box_color;
	
	color_string = prefs_manager_get_string(prefs_manager, "main_window", "event-box-color", "#f7f7be");
	g_assert(color_string != NULL);
	
	if(gdk_color_parse(color_string, &event_box_color))
	{
		gtk_widget_modify_bg(event_box, GTK_STATE_NORMAL, &event_box_color);
	}
	g_free(color_string);
	*/


void
editor_notebook_page_master_treeview_row_activated_cb(GtkTreeView *tree_view, GtkTreePath *tree_path, 
	GtkTreeViewColumn *column, gpointer user_data)
{
	/*
	g_assert(tree_view != NULL);
	g_assert(tree_path != NULL);
	g_assert(column != NULL);
	g_assert(user_data != NULL);
	
	if(gtk_tree_path_get_depth(tree_path) == 1)
	{
		if(gtk_tree_view_row_expanded(tree_view, tree_path))
			gtk_tree_view_collapse_row(tree_view, tree_path);
		else
			gtk_tree_view_expand_row(tree_view, tree_path, FALSE);
	}
	else
	{
		GtkTreeIter tree_iter;
		
		GtkTreeModel *tree_model = gtk_tree_view_get_model(tree_view);
		g_assert(tree_model != NULL);
		
		gboolean result = gtk_tree_model_get_iter(tree_model, &tree_iter, tree_path);
		g_assert(result);
		
		gchar *table_name = NULL;
		
		gtk_tree_model_get(tree_model, &tree_iter, 1, &table_name, -1);
		g_assert(table_name != NULL);
		
		GtkBuilder *builder = GTK_BUILDER(user_data);
		g_assert(builder != NULL);
		
		main_window_create_notebook_table_page(builder, table_name, TRUE);
		
		g_free(table_name);
	}
	*/
}

void
fill_master_notebook_page(GtkBuilder *builder)
{
	/*
	g_assert(builder != NULL);
	
	GObject *window = gtk_builder_get_object(builder, "main_window");
	g_assert(window != NULL);
	
	GtkTreeView *treeview = GTK_TREE_VIEW(gtk_builder_get_object(builder, "editor_notebook_page_master_treeview"));
	g_assert(treeview != NULL);
	
	sqlite3 *db = g_object_get_data(window, DATABASE_KEY);
	if(db)
	{
		gchar **result_table = NULL;
		gchar *error_message = NULL;
		gint rows = 0, cols = 0;
		
		if(sqlite3_get_table(db, "SELECT type, name, tbl_name, rootpage, sql FROM SQLITE_MASTER ORDER "
			"BY type ASC, name ASC", &result_table, &rows, &cols, &error_message)==SQLITE_OK)
		{
			// sqlite apparently gives us invalid data back if ROW = 0. So, in order for us to not segfault, make sure the rows are tested first
			if(rows == 0)
			{
				GList *columns = gtk_tree_view_get_columns(treeview);
				g_list_foreach(columns, remove_tree_view_column, treeview);
				g_list_free(columns);
			}
			else
			{
				gint i, j;
				
				GtkTreeViewColumn *col;
				GtkCellRenderer *renderer;
				
				// A check whether the columns have been previously initialized
				if(gtk_tree_view_get_column(treeview, 0) == NULL)
				{
					// Do not include the type, since that is what the treeview parent will be based on
					for(j = 1 ; j < 5; j++)
					{
						col = gtk_tree_view_column_new();
						g_assert(col != NULL);
						
						gtk_tree_view_column_set_title(col, result_table[j]);
						gtk_tree_view_append_column(treeview, col);
						
						renderer = gtk_cell_renderer_text_new();
						g_assert(renderer != NULL);
						gtk_tree_view_column_pack_start(col, renderer, TRUE);
						gtk_tree_view_column_add_attribute(col, renderer, "text", j);
					}
				}
				
				GtkTreeStore *store;
				GtkTreeIter toplevel, child;
				gchar *prev_type = NULL;
				
				store = gtk_tree_store_new(cols, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
				g_assert(store != NULL);
			
				for(i = 1; i <= rows; i++)
				{
					if(prev_type == NULL || g_strcmp0(prev_type, result_table[i*cols])!=0)
					{
						gtk_tree_store_append(store, &toplevel, NULL);
						gtk_tree_store_set(store, &toplevel, 1, result_table[i*cols], -1);
						prev_type = result_table[i*cols];
					}
				
					gtk_tree_store_append(store, &child, &toplevel);
				
					// Don't include the type
					for(j = 1; j < cols; j++)
					{
						gtk_tree_store_set(store, &child, j, result_table[i*cols+j], -1);
					}
				}
				
				gtk_tree_view_set_model(treeview, GTK_TREE_MODEL(store));
				
				g_object_unref(store);
				
				sqlite3_free_table(result_table);
			}
		}
		else
		{
			log_warning("Unable to populate treeview. %s", error_message);
			if(error_message)
				sqlite3_free(error_message);
		}
	}
	*/
}

void
main_window_notebook_tables_list_set_refresh_hint(gpointer user_data, gint page_num, gboolean hint_state)
{
	/*
	g_assert(user_data != NULL);
	g_assert(page_num >= 0);
	g_assert(main_window_notebook_is_nth_page_type(user_data, page_num, MWNPT_TABLES_LIST));
	
	MainWindowControls *controls = main_window_get_nth_page_controls(user_data, page_num, MWCT_TABLES_LIST_EVENT_BOX);
	g_assert(controls != NULL);
	
	if(hint_state)
		gtk_widget_hide(GTK_WIDGET(controls->controls.page_type_tables_list.event_box));
	else
		gtk_widget_show(GTK_WIDGET(controls->controls.page_type_tables_list.event_box));
	*/
}

void
main_window_tree_view_close_button_on_clicked(GObject *object, gpointer user_data)
{
	/*
	g_assert(user_data != NULL);
	
	GtkBuilder *builder = GTK_BUILDER(user_data);
	g_assert(builder != NULL);
	
	GtkNotebook *notebook = GTK_NOTEBOOK(gtk_builder_get_object(builder, "editor_notebook"));
	g_assert(notebook != NULL);
	
	GtkPaned *paned_box = GTK_PANED(gtk_notebook_get_nth_page(notebook, gtk_notebook_get_current_page(notebook)));
	g_assert(paned_box != NULL);
	
	GtkWidget *bottom_box = gtk_paned_get_child2(paned_box);
	g_assert(bottom_box != NULL);
	
	gtk_widget_hide_all(bottom_box);
	*/
}



gint
main_window_create_notebook_table_page(GtkBuilder *builder, const gchar *table_name, gboolean focus)
{
	/*
	g_assert(builder != NULL);
	
	GtkNotebook *notebook = GTK_NOTEBOOK(gtk_builder_get_object(builder, "editor_notebook"));
	g_assert(notebook != NULL);
	
	GtkBox *vbox = GTK_BOX(gtk_vbox_new(FALSE, 0));
	g_assert(vbox != NULL);
	
	GtkBox *rename_box = GTK_BOX(gtk_hbox_new(FALSE, 0));
	g_assert(rename_box != NULL);
	
	GtkWidget *rename_entry = gtk_entry_new();
	g_assert(rename_entry);
	
	gtk_entry_set_text(GTK_ENTRY(rename_entry), table_name);
	
	GtkWidget *rename_button = gtk_button_new_with_label("Rename Table");
	g_assert(rename_button);
	
	g_signal_connect(G_OBJECT(rename_button), "clicked", G_CALLBACK(main_window_notebook_table_page_rename), builder);
	
	gtk_box_pack_start(rename_box, rename_entry, TRUE, TRUE, 5);
	gtk_box_pack_start(rename_box, rename_button, FALSE, FALSE, 5);
	
	gtk_box_pack_start(vbox, GTK_WIDGET(rename_box), FALSE, TRUE, 5);
	
	GtkWidget *header_box = create_notebook_tab_header(table_name, G_CALLBACK(main_window_notebook_tab_close_on_clicked), builder);
	g_assert(header_box != NULL);
	
	g_object_set_data(G_OBJECT(header_box), "notebook_page", vbox);
	g_object_set_data_full(G_OBJECT(vbox), "table_name", g_strdup(table_name), (GDestroyNotify)g_free);
	
	gint result = gtk_notebook_append_page(notebook, GTK_WIDGET(vbox), header_box);
	g_assert(result != -1);
	
	gtk_widget_show_all(GTK_WIDGET(vbox));
	
	if(focus)
		gtk_notebook_set_current_page(notebook, result);
	
	return result;
	*/
	
	
	
	
	
	
	return -1;
}


gboolean
should_trigger_hint_refresh(const gchar* query)
{
	/*
	g_assert(query != NULL);
	
	if(hint_regex)
	{
		return g_regex_match(hint_regex, query, 0, NULL);
	}
	else
	{
		return FALSE;
	}
	*/
	
	
	
	
	
	return TRUE;
}

void
editor_notebook_page_master_hint_button_cancel_clicked_cb(GObject *object, gpointer user_data)
{
	/*
	g_assert(object != NULL);
	g_assert(user_data != NULL);
	
	GtkBuilder *builder = GTK_BUILDER(user_data);
	g_assert(builder != NULL);
	
	GtkWidget *event_box = GTK_WIDGET(gtk_builder_get_object(builder, "editor_notebook_page_master_hint_event_box"));
	g_assert(event_box != NULL);
	
	gtk_widget_hide(event_box);
	*/
}

void
main_window_notebook_table_page_rename(GObject *object, gpointer user_data)
{
	/*
	g_assert(object != NULL);
	g_assert(user_data != NULL);
	
	GtkWidget *notebook_page = gtk_widget_get_parent(gtk_widget_get_parent(GTK_WIDGET(object)));
	g_assert(notebook_page != NULL);
	
	GtkBuilder *builder = GTK_BUILDER(user_data);
	g_assert(builder != NULL);
	
	GtkWindow *window = GTK_WINDOW(gtk_builder_get_object(builder, "main_window"));
	g_assert(window != NULL);
	
	GtkNotebook *notebook = GTK_NOTEBOOK(gtk_builder_get_object(builder, "editor_notebook"));
	g_assert(notebook != NULL);
	
	sqlite3* db = sqlitegtk_get_database(window);
	g_assert(db != NULL);
	
	gchar* old_name = g_object_get_data(G_OBJECT(notebook_page), "table_name");
	g_assert(old_name != NULL);
	
	GtkBox *hbox = GTK_BOX(gtk_widget_get_parent(GTK_WIDGET(object)));
	g_assert(hbox != NULL);
	
	GList *children = gtk_container_get_children(GTK_CONTAINER(hbox));
	g_assert(children != NULL);
	
	GtkEntry *entry = GTK_ENTRY(g_list_nth_data(children, 0));
	g_assert(entry != NULL);
	
	g_list_free(children);
	
	const gchar* new_name = gtk_entry_get_text(entry);
	g_assert(new_name != NULL);
	
	gchar *query = g_strdup_printf("ALTER TABLE \"%s\" RENAME TO \"%s\"", old_name, new_name);
	g_assert(query != NULL);
	
	gchar **result_table = NULL, *error_message = NULL;
	gint rows, cols;
	
	if(sqlite3_get_table(db, query, &result_table, &rows, &cols, &error_message)==SQLITE_OK)
	{
		trigger_hint_refresh(builder);
		
		gchar *new_name_copy = g_strdup(new_name);
		g_assert(new_name_copy != NULL);
		g_object_set_data_full(G_OBJECT(notebook_page), "table_name", new_name_copy, (GDestroyNotify)g_free);
		
		GtkWidget *notebook_page_label = gtk_notebook_get_tab_label(notebook, notebook_page);
		g_assert(notebook_page_label != NULL);
		
		change_notebook_page_label(notebook_page_label, new_name);
	}
	else
	{
		log_warning("Unable to rename. Error %s", error_message);
	}
	if(result_table)
		sqlite3_free_table(result_table);
	if(error_message)
		sqlite3_free(error_message);
		*/

}
